package cn.hwadee.muying.service;

import cn.hwadee.muying.commons.utils.DateTools;
import cn.hwadee.muying.dto.Client;
import cn.hwadee.muying.entity.CareRecord;
import cn.hwadee.muying.mapper.CareRecordMapper;
import cn.hwadee.muying.commons.base.BaseService;
import cn.hwadee.muying.mapper.ClientMapper;
import cn.hwadee.muying.mapper.UserMapper;
import org.springframework.stereotype.Service;
import cn.hwadee.muying.entity.User;
import cn.hwadee.muying.commons.page.MyPage;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.List;

/**
 * <p>
 * 关怀记录 服务实现类
 * </p>
 *
 * @author SuWuqiang
 * @since 2018-08-08
 */
@Service
public class CareRecordService extends BaseService<CareRecordMapper, CareRecord> {

    @Autowired
    private CareRecordMapper careRecordMapper;

    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private UserService userService;

    public MyPage<CareRecord> list(MyPage<CareRecord> page, User cUser, Timestamp start, Timestamp end, String keyword) throws Exception{
        List<CareRecord> careRecords = careRecordMapper.list(page,cUser,start,end,keyword);
        for(CareRecord careRecord:careRecords){
            User user;
            user = userService.selectById(careRecord.getReceiveId());
            careRecord.setClientName(user.getUsername());
            careRecord.setClientAccount(user.getAccount());
            user = userService.selectById(careRecord.getWorkerId());
            careRecord.setWorkerName(user.getUsername());
            careRecord.setSendDate(careRecord.getCreateDate().toString());
            if(careRecord.getStatus()){
                careRecord.setResult("成功");
            }else {
                careRecord.setResult("失败");
            }
        }
        page.setRecords(careRecords);
        return page;
    }

    public MyPage<Client> send(MyPage<Client> page, User cUser, Boolean old, String keyword) throws Exception{
        page.setOrderByField("b.updateDate");
        List<Client> list = clientMapper.list(page,cUser,old,keyword);
        for(Client client:list){
            if(client.getBirthday()!=null){
                client.setBirthdayString(DateTools.dayFormat.format(client.getBirthday()));
            }
        }
        page.setRecords(list);
        return page;
    }

}
